/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SocketBusiness;

import MyLogging.ILog;
import DTO.PacketDTO;
import CommonValue.SystemTimer;
import DTO.ControlDTO;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public abstract class ClientConnect implements Runnable {

    private String defaultServerIP;
    private int defaultPort;
    private Socket client;
    private OutputStream outStream;
    private InputStream inStream;
    private String systemSign;
    private PacketDTO myPacketDTO;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private ControlDTO operation;

    public ClientConnect(String systemSign, String ServerIP, int port,ControlDTO operation) {
        this.defaultServerIP = ServerIP;
        this.defaultPort = port;
        this.systemSign = systemSign;
        this.operation = operation;
    }

    public boolean openSocket() {
        try {
            client = new Socket(defaultServerIP, defaultPort);
            return true;
        } catch (UnknownHostException ex) {
            ILog.writeLog("UnknownHostException", systemSign);
            ILog.writeLog("Waiting ...", systemSign);
            return false;
        } catch (IOException ex) {
            ILog.writeLog("Waiting ...", systemSign);
            ILog.writeLog("IOException", systemSign);
            return false;
        }

    }

    public boolean openClient() {
        try {
            int count = 0;
            int maxCount = 300;
            while (!openSocket()) { // Try to open socket 300 times to out

                try {
                    Thread.sleep(SystemTimer.WaitToConnectSleep);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ClientConnect.class.getName()).log(Level.SEVERE, null, ex);
                }

                count++;
                if (count > maxCount) {
                    ILog.writeLog("Waiting timeout", systemSign);
                    break;
                }
            }
            try {
//                inStream = new DataInputStream(client.getInputStream());
//                ois = new ObjectInputStream(inStream);

                outStream = new DataOutputStream(client.getOutputStream());
                oos = new ObjectOutputStream(outStream);
            } catch (Exception e) {
                System.out.println("exception !!!");
            }

            ILog.writeLog("Connected to server at port " + defaultPort, systemSign);

            return true;
        } catch (Exception e) {
            ILog.writeLog("Open Client Failure", systemSign);
        }
        return false;
    }

    public void closeClient() {
        try {
            ois.close();
            oos.close();
            inStream.close();
            outStream.close();
            client.close();
        } catch (IOException e) {
            ILog.writeLog("Close Client Failure", systemSign);
        }
    }

    public void business() {
        ILog.writeLog("Business Not Installed", systemSign);
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object receiveMessage() throws ClassNotFoundException {
        try {
            return this.ois.readObject();
        } catch (IOException ex) {
            ILog.writeLog("Receive Message ERROR !!! ", systemSign);
        }
        return "";
    }

    public boolean sendMessage(Object ob) throws ClassNotFoundException {

        try {
            System.out.println(this.getClass() + " CHUAN BI SEND OBJECT : >>>>>>>>>>>>>>>>>> : " + ob.getClass().toString());
            oos.writeObject(ob);
            oos.reset();
            oos.flush();
            return true;
        } catch (IOException ex) {
            ILog.writeLog("Send Message ERROR !!! ", systemSign);
            return false;
        }

    }

    @Override
    public void run() {
        if (!openClient()) {
            return;
        }
            business();
        closeClient();
    }

    public Socket getClient() {
        return client;
    }

    public void setClient(Socket client) {
        this.client = client;
    }

    public int getDefaultPort() {
        return defaultPort;
    }

    public void setDefaultPort(int defaultPort) {
        this.defaultPort = defaultPort;
    }

    public String getDefaultServerIP() {
        return defaultServerIP;
    }

    public void setDefaultServerIP(String defaultServerIP) {
        this.defaultServerIP = defaultServerIP;
    }

    public InputStream getInStream() {
        return inStream;
    }

    public void setInStream(InputStream inStream) {
        this.inStream = inStream;
    }

    public ObjectInputStream getOis() {
        return ois;
    }

    public void setOis(ObjectInputStream ois) {
        this.ois = ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    public void setOos(ObjectOutputStream oos) {
        this.oos = oos;
    }

    public OutputStream getOutStream() {
        return outStream;
    }

    public void setOutStream(OutputStream outStream) {
        this.outStream = outStream;
    }

    public String getSystemSign() {
        return systemSign;
    }

    public void setSystemSign(String systemSign) {
        this.systemSign = systemSign;
    }

    public PacketDTO getMyPacketDTO() {
        return myPacketDTO;
    }

    public void setMyPacketDTO(PacketDTO myPacketDTO) {
        this.myPacketDTO = myPacketDTO;
    }

    public ControlDTO getOperation() {
        return operation;
    }

    public void setOperation(ControlDTO operation) {
        this.operation = operation;
    }
    
    
}
